<?php

namespace app\validate;

use think\Validate;

class Admin extends Validate
{
    protected $pk = 'id';
    protected $rule = [
        'username'  => 'require|unique:admin|length:5,200|alphaNum',
        'password'  => 'require',
        'nickname'  => 'require|length:2,200',
        'code'      => 'require|valiCode',
        'role'      => 'require',
        'type' => 'require|in:1,2,3,4',
    ];

    protected $scene = [
        'edit'  => ['username', 'nickname', 'role', 'type'],
        'add'   => ['username', 'nickname', 'password', 'role', 'type'],
    ];

    protected $message = [
        'password.require'   => '必须填写密码',
        'role.require'       => '必须选择角色',
        'code.require'       => '必须填验证码',
        'nickname.require' => '备注名称不能为空',
        'nickname.length' => '备注长度不能低于5个字符',
        'username.require' => '用户账号不能为空',
        'username.alphaNum' => '用户账号只能是数字或字母',
        'username.length' => '用户账号长度不能低于5个字符',
        'type.require' => '账号类型不能为空',
        'type.in' => '账号类型不正确'
    ];

    protected function valiCode($value)
    {
        return captcha_check($value) ? true : '验证码错误';
    }

    public function scenelogin()
    {
        return $this->only(['username', 'password', 'code'])->remove('username', 'unique');
    }
}
